{extend name="base"/}
{block name="resources"}
<style>
	.layui-input-inline.js-pid a{
		margin-left: 20px;
	}
	.ns-form {margin-top: 0;}
</style>
{/block}
{block name="main"}
<div class="layui-form ns-form">
	
	<div class="layui-form-item">
		<label class="layui-form-label">默认商品图片：</label>
		<div class="layui-input-inline">
			<div class="upload-img-block">
				<div class="upload-img-box {if !empty($default_img.default_goods_img)}hover{/if}">
					<div class="ns-upload-default" id="imgUploadGoods">
					{if empty($default_img.default_goods_img)}
						<div class="upload">
							<img layer-src src="SHOP_IMG/upload_img.png" />
							<p>点击上传</p>
						</div>
					{else/}
						<div id="preview_imgUploadGoods" class="preview_img">
							<img layer-src src="{:img($default_img.default_goods_img)}" alt="" class="img_prev">
						</div>
					{/if}
					</div>
					<div class="operation"  >
						<div >
							<i title="图片预览" class="iconfont iconreview js-preview" style="margin-right: 20px;"></i>
							<i title="删除图片" class="layui-icon layui-icon-delete js-delete"></i>
						</div>
						<div class="replace_img js-replace">点击替换</div>
					</div>
					<input type="hidden" name="default_goods_img" {if $default_img} value="{$default_img.default_goods_img}" {/if}>
				</div>
			<!-- 	<p id="imgUploadGoods" class=" {if condition="$default_img.default_goods_img"} replace {else/} no-replace{/if}">替换</p>
				<i class="del {if !empty($default_img.default_goods_img)}show{/if}">x</i> -->
			</div>
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label">默认会员头像：</label>
		<div class="layui-input-inline">
			<div class="upload-img-block">
				<div class="upload-img-box {if !empty($default_img.default_goods_img)}hover{/if}">
					<div class="ns-upload-default" id="imgUploadMember">
					{if empty($default_img.default_headimg)}
						<div class="upload">
							<img layer-src src="SHOP_IMG/upload_img.png" />
							<p>点击上传</p>
						</div>
					{else/}
						<div id="preview_imgUploadMember" class="preview_img">
							<img layer-src src="{:img($default_img.default_headimg)}" alt="" class="img_prev">
						</div>
					{/if}
					</div>
					<div class="operation"  >
						<div >
							<i title="图片预览" class="iconfont iconreview js-preview" style="margin-right: 20px;"></i>
							<i title="删除图片" class="layui-icon layui-icon-delete js-delete"></i>
						</div>
						<div class="replace_img js-replace">点击替换</div>
					</div>
					<input type="hidden" name="default_headimg" {if $default_img} value="{$default_img.default_headimg}"{/if}>
				</div>
				<!-- <p id="imgUploadMember" class=" {if condition="$default_img.default_headimg"} replace {else/} no-replace{/if}">替换</p>
				<i class="del {if !empty($default_img.default_headimg)}show{/if}">x</i> -->
			</div>
		</div>
	</div>
	<div class="ns-form-row">
		<button class="layui-btn ns-bg-color" lay-submit lay-filter="save">保存</button>
	</div>
</div>
{/block}
{block name="script"}
<script>
	layui.use(['form'], function() {
		var form = layui.form,
			repeat_flag = false;//防重复标识
		form.render();

		var goods_upload = new Upload({
			elem: '#imgUploadGoods'
		});

		var member_upload = new Upload({
			elem: '#imgUploadMember'
		});

        form.on('submit(save)', function(data) {
            if (repeat_flag) return false;
            repeat_flag = true;

			// 删除图片
			if(!data.field.default_goods_img && goods_upload.path != 'upload/default/default_img/goods.png') goods_upload.delete();

			if(!data.field.default_headimg && goods_upload.path != 'upload/default/default_img/head.png') member_upload.delete();

            $.ajax({
                type: "POST",
                url: ns.url("shop/config/defaultpicture"),
                data: data.field,
                dataType: 'JSON',
                success: function(res) {
                    repeat_flag = false;
                    if (res.code == 0) {
                        layer.msg(res.message);
                        location.reload();
                    }else{
                        layer.msg(res.message);
                    }
                }
            });
        });

	});
</script>
{/block}